# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
class NestedInteger:
    def __init__(self,value):
        self.value = value
    def isInteger(self) -> bool:
       """
       @return True if this NestedInteger holds a single integer, rather than a nested list.
       """
       if type(self.value) is int:
           return True
       else:
           return False

    def getInteger(self) -> int:
       """
       @return the single integer that this NestedInteger holds, if it holds a single integer
       Return None if this NestedInteger holds a nested list
       """
       return self.value

    def getList(self):
       """
       @return the nested list that this NestedInteger holds, if it holds a nested list
       Return None if this NestedInteger holds a single integer
       """
       return self.value

    def __len__(self):
        return len(self.value)

class NestedIterator:
    def __init__(self, nestedList):
        self.stack = []
        for i in range(len(nestedList)-1,-1,-1):
            self.stack.append(nestedList[i])

    def next(self) -> int:
        node = self.stack.pop(-1)
        while not node.isInteger():
            nestedList = node.getList()
            for i in range(len(nestedList)-1,-1,-1):
                self.stack.append(nestedList[i])
            node = self.stack.pop(-1)
        return node

    def hasNext(self) -> bool:
        while len(self.stack)>0 and not self.stack[-1].isInteger():
            node = self.stack.pop(-1)
            nestedList = node.getList()
            for i in range(len(nestedList) - 1, -1, -1):
                self.stack.append(nestedList[i])
        return len(self.stack)>0


# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())

if __name__ == '__main__':
    nestedList = [[1,1],2,[1,1]]
    a = [nestedList]
    while len(a):
        node = a.pop(-1)
        for i in range(len(node)):
            if type(node[i]) is list:
                a.append(node[i])
                node[i] = NestedInteger(node[i])
            node[i] = NestedInteger(node[i])
    i = NestedIterator(nestedList)
    while i.hasNext():
        print(i.next().value)


